package com.lzh.chatnote.user.controller;

import com.lzh.chatnote.user.config.loginConfig.GiteeConfigProperties;
import com.lzh.chatnote.user.domain.entity.AuthData;
import com.lzh.chatnote.user.service.ThreeLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthQqRequest;
import me.zhyd.oauth.request.AuthRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "第三方登录控制器qq，github,gitee")
@RestController
public class ThreeLoginController {
    @Resource
    private GiteeConfigProperties giteeConfigProperties;
    @Resource
    private ThreeLoginService threeLoginService;

    /**
     * 第三方登录回调
     */
    @ApiOperation(value = "第三方登录回调")
    @GetMapping("/callback/{source}")
    public void callBackAuth(AuthCallback callback, HttpServletResponse response, @PathVariable String source) {
        AuthRequest authRequest = getAuthRequest(source);
        AuthResponse<AuthData> authResponse = (AuthResponse<AuthData>) authRequest.login(callback);
        threeLoginService.login(response, source, authResponse, callback.getState());
    }

    /**
     * 获取第三方登录请求
     *
     * @param source 登录来源
     * @return AuthRequest
     */
    private AuthRequest getAuthRequest(String source) {
        AuthRequest authRequest = null;
        switch (source) {
            case "gitee":
                authRequest = new AuthGiteeRequest(AuthConfig
                        .builder()
                        .clientId(giteeConfigProperties.getAppId())
                        .clientSecret(giteeConfigProperties.getAppSecret())
                        .redirectUri(giteeConfigProperties.getRedirectUrl())
                        .build()
                );
                break;
            case "github":
                authRequest = new AuthGithubRequest(AuthConfig
                        .builder()
                        .clientId(giteeConfigProperties.getAppId())
                        .clientSecret(giteeConfigProperties.getAppSecret())
                        .redirectUri(giteeConfigProperties.getRedirectUrl())
                        .build()
                );
                break;
            case "qq":
                authRequest = new AuthQqRequest(AuthConfig
                        .builder()
                        .clientId(giteeConfigProperties.getAppId())
                        .clientSecret(giteeConfigProperties.getAppSecret())
                        .redirectUri(giteeConfigProperties.getRedirectUrl())
                        .build()
                );
                break;
            default:
                break;
        }
        return authRequest;
    }
}
